﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using Microsoft.Drawing;
using System.Drawing;
using System.IO;
using TouchRC.Logic;

namespace TouchRC.Custom_Controls
{
    public class TouchButton : FluidLabel
    {
        private string[] m_ValidImages = new string[] { ".png", ".jpg", ".gif", ".bmp" };

        #region Private Members

        private IImage m_Image;
        private Size m_ImageBounds;
        private bool m_MaintainImageAspectRatio = true;

        #endregion

        public TouchButton()
            :base()
        {
        }

        public TouchButton(int x, int y, int width, int height)
            : base(string.Empty, x, y, width, height)
        {
        }

        ~TouchButton()
        {
            Dispose();
        }

        public IImage Image 
        { 
            get { return m_Image; } 
            set 
            { 
                m_Image = value;
                Invalidate();
            } 
        }

        public Size ImageBounds 
        {
            get
            {
                if (m_ImageBounds == null || m_ImageBounds == Size.Empty)
                    return Bounds.Size;
                else
                    return m_ImageBounds;
            }
            set { m_ImageBounds = value; }
        }

        public override bool Active { get { return true; } }

        public bool MaintainImageAspectRatio { get { return m_MaintainImageAspectRatio; } set { m_MaintainImageAspectRatio = value; } }

        protected override void OnPaintBackground(FluidPaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            Rectangle rect = new Rectangle();

            int width = ImageBounds.Width;
            int height = ImageBounds.Height;
            int x = e.ControlBounds.X;
            int y = e.ControlBounds.Y;

            if (m_MaintainImageAspectRatio)
            {
                ImageInfo img;
                m_Image.GetImageInfo(out img);//etPhysicalDimension(out imageSize);

                double heightScale = (double)img.Height / height;
                double widthScale = (double)img.Width / width;

                if (heightScale < widthScale)
                {
                    double ratio = (double)img.Height/ img.Width;
                    height = (int)(width * ratio);
                }
                else
                {
                    double ratio = (double)img.Width / img.Height;
                    width = (int)(height * ratio);
                }

                x = x + ((ImageBounds.Width - width) / 2);
                y = y + ((ImageBounds.Height - height) / 2);
            }

            rect = new Rectangle(x, y, width, height);//ScaleX(width), ScaleY(height));

            graphics.DrawImageAlphaChannel(m_Image, rect);
        }

        public override void Dispose()
        {
            m_Image = null;
            base.Dispose();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }
    }
}
